import java.text.SimpleDateFormat
import java.util.Date

import org.apache.spark.sql.SQLContext
import org.apache.spark.{SparkConf, SparkContext}

/**
  * @ Auther: o2o-rd-0008
  * @ Date:   2019/9/4 10:23
  * @ Param:  ${PARAM}
  * @ Description:
  */
object test_cate {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
    conf.setAppName(s"${this.getClass.getSimpleName}")
    conf.set("spark.debug.maxToStringFields", "500")
    conf.setMaster("local[*]")
    conf.set("es.nodes", "192.168.1.157")
    conf.set("es.port", "9200")
    conf.set("cluster.name", "O2OElastic")
    conf.set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
    conf.set("spark.sql.caseSensitive", "true")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    sc.setLogLevel("WARN")
    sc.hadoopConfiguration.set("fs.s3a.access.key", "GAO7EO9FWKPJ8WFCQDME")
    sc.hadoopConfiguration.set("fs.s3a.secret.key", "LZ0xaHBSYKHaJ9ECDbX9f7zin79UZkXfGoNapRPL")
    sc.hadoopConfiguration.set("fs.s3a.endpoint", "https://obs.cn-north-1.myhuaweicloud.com")

    val anchor = sqlContext.read.json(s"s3a://o2o-dataproces-group/panzonghao/tmall/11/tmall_anchor_id")
    anchor.createOrReplaceTempView("anchor")
    for (months <- 1 to 10) {
      val zhibo = sqlContext.read.orc(s"s3a://o2o-dataproces-group/panzonghao/tmall/${months}/zhibo_zhubo")
      zhibo.createOrReplaceTempView("zhibo")

      sqlContext.sql(
        s"""
           |select
           |t1.*,
           |case
           |when t2.anchorId is null then t1.nick
           |else t2.nick
           |end as nicks
           |from zhibo t1
           |left join
           |anchor t2
           |on t1.anchorId=t2.anchorId
         """.stripMargin).drop("nick").withColumnRenamed("nicks", "nick")
        .repartition(4).write.orc(s"s3a://o2o-dataproces-group/panzonghao/tmall/${months}/zhibo_zhubo_v1")
    }

  sc.stop()
  }

  def tranTimeToString(timestamp:String) :String={
    val tm = timestamp+"000"
    val fm = new SimpleDateFormat("yyyy-M")
    val time = fm.format(new Date(tm.toLong))
    time
  }
}
